package com.yanyeori.framework.core.util;

import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

/**
 * AES对称算法加密/解密工具类
 *
 * @author chenkuan 2020/8/4
 */
public class AESUtil {

    private static final String AES = "AES";
    private static final String AES_ECB = "AES/ECB/PKCS5Padding";
    private static final String AES_CBC = "AES/CBC/PKCS5Padding";

    /**
     * ECB加密
     *
     * @param key   Base64编码的密钥字符串
     * @param input 待加密的字符串
     * @return 加密后的Base64编码字符串
     */
    public static String encrypt(String key, String input) {
        try {
            Cipher cipher = Cipher.getInstance(AES_ECB);
            SecretKeySpec secretKeySpec = new SecretKeySpec(Base64Utils.decodeFromString(key), AES);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] bytes = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
            return Base64Utils.encodeToString(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * ECB解密
     *
     * @param key   Base64编码的密钥字符串
     * @param input 待解密的Base64编码字符串
     * @return 解密后的字符串
     */
    public static String decrypt(String key, String input) {
        try {
            Cipher cipher = Cipher.getInstance(AES_ECB);
            SecretKeySpec secretKeySpec = new SecretKeySpec(Base64Utils.decodeFromString(key), AES);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] decryptedBytes = cipher.doFinal(Base64Utils.decodeFromString(input));
            return StringUtil.str(decryptedBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * CBC加密
     *
     * @param key   Base64编码的密钥字符串
     * @param iv    Base64编码的向量字符串
     * @param input 待加密的字符串
     * @return 加密后的Base64编码字符串
     */
    public static String encrypt(String key, String iv, String input) {
        try {
            Cipher cipher = Cipher.getInstance(AES_CBC);
            SecretKeySpec secretKeySpec = new SecretKeySpec(Base64Utils.decodeFromString(key), AES);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(Base64Utils.decodeFromString(iv)));
            byte[] bytes = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
            return Base64Utils.encodeToString(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * CBC解密
     *
     * @param key   Base64编码的密钥字符串
     * @param iv    Base64编码的向量字符串
     * @param input 待解密的Base64编码字符串
     * @return 解密后的字符串
     */
    public static String decrypt(String key, String iv, String input) {
        try {
            Cipher cipher = Cipher.getInstance(AES_CBC);
            SecretKeySpec secretKeySpec = new SecretKeySpec(Base64Utils.decodeFromString(key), AES);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(Base64Utils.decodeFromString(iv)));
            byte[] decryptedBytes = cipher.doFinal(Base64Utils.decodeFromString(input));
            return StringUtil.str(decryptedBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
